package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.common.utils.BeanUtils;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.parkinglot.dto.PDADto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotDevicePdaReturnCodeDto;
import com.anjubao.parking.municipal.parkinglot.form.PDAQueryForm;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotDevicePdaAddForm;
import com.anjubao.parking.municipal.parkinglot.form.ParkingLotDevicePdaEditForm;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDevicePda;
import com.anjubao.parking.municipal.parkinglot.service.ParkingLotDevicePdaService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * PDA手持机管理基本API
 * @author anjubao.com
 *
 */


@RestController
@RequestMapping("/parking-lot-device-pdas")
@Api(value="ParkingLotDevice-PDA模块",tags = {"PDA手持机管理接口"})
public class ParkingLotDevicePdaController {

    private static final Logger logger = LoggerFactory.getLogger(ParkingLotDevicePdaController.class);

    @Autowired
    private ParkingLotDevicePdaService parkingLotDevicePdaService;

    /**
     * 创建PDA手持机设备
     *
     * @param form
     * @return
     */
    @PostMapping(value = "/")
    @ApiOperation(value = "创建PDA手持机",notes = "",code = 2000)
    @ApiImplicitParams({
            @ApiImplicitParam(name="form",required = true,dataType = "ParkingLotDevicePdaAddForm",paramType = "body")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    public ApiResult<ParkingLotDevicePdaReturnCodeDto> createParkingLotDevicePda(@RequestBody @Valid ParkingLotDevicePdaAddForm form, BindingResult result){
        if (result.hasErrors()){
            List<ObjectError> ls=result.getAllErrors();
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), String.valueOf(ls.get(0)));
        }
        logger.info("新增PDA设备 {} ", form);
        ParkingLotDevicePda entityCreated = parkingLotDevicePdaService.saveParkingLotDevicePdaService(BeanUtils.transform(ParkingLotDevicePda.class, form));

        return ApiResult.success(new ParkingLotDevicePdaReturnCodeDto().setParkingLotDevicePdaCode(entityCreated.getCode()));
    }


    @PutMapping("/{parkingLotDevicePdaCode}")
    @ApiModelProperty(value = "修改PDA手持设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name="form",required = true,dataType = "ParkingLotDevicePdaEditForm",paramType = "body"),
            @ApiImplicitParam(name="parkingLotDevicePda",value = "PDA手持设备code",required = true,dataType = "String",paramType = "path")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败")
    })
    /**
     * 更新手持设备
     *
     * @param parkingLotDevicePda
     * @param form
     * @return
     */
    public ApiResult<ParkingLotDevicePdaReturnCodeDto> updatePDA(@PathVariable String parkingLotDevicePdaCode, @RequestBody @Valid ParkingLotDevicePdaEditForm form) {
        logger.info("updating parking lot {} ", form);
        ParkingLotDevicePda parkingLotDevicePda = BeanUtils.transform(ParkingLotDevicePda.class, form);
        parkingLotDevicePda.setCode(parkingLotDevicePdaCode);
        ParkingLotDevicePda updated = parkingLotDevicePdaService.saveParkingLotDevicePdaService(parkingLotDevicePda);
        return ApiResult.success(new ParkingLotDevicePdaReturnCodeDto().setParkingLotDevicePdaCode(updated.getCode()));
    }

    @GetMapping("")
    @ApiOperation(value = "查询PDA信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pdaStatus",value = "PDA状态(1:正常，0:禁用，-1:故障)", dataType = "int" ,paramType = "query",allowableValues = "-1,0,1",type = "integer")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    /**
     * 查询
     *
     * @param form
     * @return
     */
    public ApiResult<List<PDADto>> getPDAs(PDAQueryForm form) {
        if(form.getCityId()==null&&form.getDistrictId()==null&&form.getProvinceId()==null){
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), "省市区ID不允许同时为空");
        }
        logger.info("查询视频桩信息 {}", form);
        Page<PDADto> page=new Page<>();
        page.setSize(form.getPageSize());
        page.setCurrent(form.getPage());
        IPage<PDADto> pdAs = parkingLotDevicePdaService.getPDAs(page,form);
        return ApiResult.success(pdAs.getRecords(), (int) pdAs.getTotal());
    }

    @GetMapping("/{PDACode}")
    @ApiOperation(value = "查看单个PDA接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="PDACode",value = "pda-code",required = true,dataType = "String",paramType = "path")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    /**
     * 查询
     *
     * @param videoPileCode    PDA编号
     * @return
     */
    public ApiResult<PDADto> getVideoPile(@PathVariable String PDACode) {
        logger.info("获取单个PDA {}", PDACode);
        ParkingLotDevicePda pda = parkingLotDevicePdaService.getPDA(MunicipalParkingUtils.getIdByCode(PDACode));
        PDADto pdaDto=BeanUtils.transform(PDADto.class,pda);
        pdaDto.setCode(pda.getCode());
        return ApiResult.success(pdaDto);
    }


    @ApiOperation("删除PDA接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "PDACode", value = "PDA-code", required = true, dataType = "string", paramType = "path")
    })
    @DeleteMapping("/{PDACode}")
    public ApiResult deleteVideoPileCode(@PathVariable String PDACode) {
        parkingLotDevicePdaService.deletePDA(MunicipalParkingUtils.getIdByCode(PDACode));
        return ApiResult.success();
    }

















}
